package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.MenuCondition;
import com.spa.infrastructure.repository.mapper.MenuMapper;
import com.spa.infrastructure.repository.po.MenuPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Menu仓储层.
 *
 * @author liangjun
 */
@Component
@Slf4j
public class MenuRepositoryService extends ServiceImpl<MenuMapper, MenuPO> {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 仓储处 新增菜单.
     *
     * @param menuPO 入参
     */
    public int addMenu(MenuPO menuPO) {
        return menuMapper.insert(menuPO);
    }

    public List<MenuPO> list(MenuCondition condition) {
        LambdaQueryWrapper<MenuPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), MenuPO::getId, condition.getIds());
        return list(wrapper);
    }

    public Page<MenuPO> page(MenuCondition condition) {
        LambdaQueryWrapper<MenuPO> wrapper = Wrappers.lambdaQuery();
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }

}
